package com.wu.huoshan.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wu.util.ConMysql;
import com.wu.util.HttpUtil;
import org.apache.log4j.Logger;

public class VideoService {
    private static Logger logger = Logger.getLogger(VideoService.class);
    private ConMysql conMysql = new ConMysql();

    public void caiLie() {
        // 获取采集链接
        String url = "http://hotsoon.snssdk.com/hotsoon/feed/?type=video";

        // 读取内容封装vedio
        String body = HttpUtil.sendGet(url);
        JSONObject bodyJs = JSON.parseObject(body);
        if (bodyJs != null && bodyJs.getInteger("status_code") == 0) {
            String insertSql = "insert into huo_video (huo_id,video_id,title,description,tips,play_count,share_count,digg_count,comment_count,create_time,status)  values";
            for (int i = 0; i < bodyJs.getJSONArray("data").size(); i++) {
                JSONObject js = bodyJs.getJSONArray("data").getJSONObject(i).getJSONObject("data");
                // 检查id
                if (!conMysql.checkId("select id from huo_video where huo_id=\"" + js.getString("id") + "\" limit 1")) {
                    String values = "(\"" + js.getString("id") + "\",\"" + js.getJSONObject("video").getString("video_id") + "\",\"" + js.getString("title") + "\",\"" +
                            js.getString("description") + "\",\"" + js.getString("tips").replace("火力: ", "") + "\"," + js.getJSONObject("stats").getInteger("play_count") + "," +
                            js.getJSONObject("stats").getInteger("share_count") + "," + js.getJSONObject("stats").getInteger("digg_count") + "," + js.getJSONObject("stats").getInteger("comment_count") + ",\"" +
                            js.getLong("create_time") + "\",1)";
                    if (i == bodyJs.getJSONArray("data").size() - 1) {
                        insertSql = insertSql + values;
                    } else {
                        insertSql = insertSql + values + ",";
                    }

                }
            }

            try {
                logger.info("插入" + bodyJs.getJSONArray("data").size() + "视频成功");
                if (bodyJs.getJSONArray("data").size() == conMysql.insertSql(insertSql)) {
                    logger.info("插入" + bodyJs.getJSONArray("data").size() + "视频成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
